package cn.ekuma.data.ui.android.editor;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.openbravo.data.user.BaseBeanListRender;
import com.openbravo.data.user.I_AndroidBrowsableEditableData;
import com.openbravo.data.user.ListDataEvent;
import com.openbravo.data.user.ListDataListener;

/**
 * Created by Administrator on 2014/12/11.
 */
public class BrowsableEditableDataAdapter  extends BaseAdapter implements ListDataListener {

    protected I_AndroidBrowsableEditableData m_db;
    BaseBeanListRender render;
    Context c;

    public  BrowsableEditableDataAdapter(Context c,I_AndroidBrowsableEditableData m_db,BaseBeanListRender render){
         this.m_db=m_db;
        m_db.addListDataListener(this);
         this.c=c;
        this.render=render;
    }

    @Override
    public int getCount() {
        return m_db.getCount();
    }

    @Override
    public Object getItem(int position) {
        return m_db.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return m_db.getItemId(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(c).inflate(android.R.layout.simple_list_item_1, null);

        TextView text = (TextView) view;
        Object item = getItem(position);
        text.setText(render.getRenderString(item));

        return view;
    }

    @Override
    public void intervalAdded(ListDataEvent e) {

        System.out.println("监听到插入事件：");
        notifyDataSetChanged();
    }

    @Override
    public void intervalRemoved(ListDataEvent e) {
       notifyDataSetChanged();
    }

    @Override
    public void contentsChanged(ListDataEvent e) {
       notifyDataSetInvalidated();
    }
}
